# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  #layout proc{ |c| c.request.xhr? ? false : "application" }
  #layout proc{ |c| c.request.xhr? ? sub_layout : "application" }
  layout :layout

  helper :all # include all helpers, all the time
  #before_filet :check_for_search_engine
  before_filter :init

  # Better to do as a scheduled task and parse the web server log,
  # then post to database
  # example google string:
  # http://www.google.se/search?hl=sv&client=firefox-a&channel=s&rls=org.mozilla:sv-SE:official&ei=tbEpSuHQGMbR-QbkqayFCQ&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=g%C3%B6ra+om+java+till+exe&spell=1
  #def check_for_search_engine
  #  if request.get?
  #    Searchquery.query_issued(@query)
  #  end
  #end

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery  :secret => '96f86d53ad5b537e40f0faed86d20112'
  
  # See ActionController::Base for details 
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password"). 
  # filter_parameter_logging :password

  def init
    @page_title = "Trading on Rails"
    @provinces = Province.find(:all,:order => "name")
  end

  def layout
    #request.xhr? ? sub_layout : "application"
    params[:partial] ? sub_layout : "application"
  end

  def sub_layout
    "layouts/default"
  end


  def login_required
    if session[:user]
      return true
    end

    #session[:return_to] = request.request_uri
    session[:return_to] = "/#{params[:controller]}/#{params[:action]}"
    if params[:partial]
      redirect_to "/partial/auth/login"
    else
      redirect_to "/auth/login"
    end
    return false
  end

  def spam_required
    if session[:user] || session[:spam_trusted]
      return true
    end
    
    session[:params] = request.parameters
    session[:spam_return]=request.request_uri
    redirect_to :controller => "spam", :action => "start"
    return false
  end

  def load_params_from_session
    if session[:params]
      self.params = session[:params]
      session[:params] = nil
    end
  end

  def online
    if session[:user]
      return true
    end
    return false
  end
end
